package es.upv.epsa.mobweb.moneyback;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import es.upv.epsa.mobweb.moneyback.json.JSONParser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	
	private Button buttonOk;
	private EditText editTextName;
	private EditText editTextUserName;
	private EditText editTextPassword;
	private EditText editTextMobile;
	private EditText editTextEmail;
	
	private JSONParser jsonParser;
	private String		server_url;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		jsonParser = new JSONParser();
		setWidgets();
	}
	
	private void setWidgets(){
		server_url		= getResources().getString(R.string.server_url) +
		  		  		  getResources().getString(R.string.users_url);
		
		buttonOk = (Button) findViewById(R.id.buttonOk);
		
		buttonOk.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View arg0) {
				registerUser();
			}
			
		});
		
		editTextName = (EditText) findViewById(R.id.editTextName);
		editTextUserName = (EditText) findViewById(R.id.editTextUserName);
		editTextPassword = (EditText) findViewById(R.id.editTextPassword);
		editTextMobile = (EditText) findViewById(R.id.editTextMobile);
		editTextEmail = (EditText) findViewById(R.id.editTextEmail);

	}
	
	// COPY FROM UserLoginActivty Starts here
	private void registerUser(){
    	JsonFactory 			jfactory 	= new JsonFactory();
		ByteArrayOutputStream 	bout 		= new ByteArrayOutputStream();
		JsonGenerator 			jGenerator;
		
		String name = editTextName.getText().toString();
		String username = editTextUserName.getText().toString();
		String password = editTextPassword.getText().toString();
		String mobile = editTextMobile.getText().toString();
		String email = editTextEmail.getText().toString();
		
		try {
			jGenerator = jfactory.createJsonGenerator(new OutputStreamWriter(bout, "UTF-8"));
			jGenerator.writeStartObject();

			jGenerator.writeNumberField("action", 1);
			jGenerator.writeStringField("name", name);
			jGenerator.writeStringField("username", username);
			jGenerator.writeStringField("password", password);
			jGenerator.writeStringField("mobile", mobile);
			jGenerator.writeStringField("email", email);
			
			jGenerator.writeEndObject();
			jGenerator.close();
			
			new SendJSON(bout.toString()).execute();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	// COPY FROM UserLoginActivty Ends here

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}
	
	// COPY FROM UserLoginActivty Starts here
	private class SendJSON extends AsyncTask<Void, Void, Void>{
		String 			json 	= "";
		String 			data 	= "";
		ProgressDialog 	dialog;
		
		public SendJSON(String json){
			this.json = json;
		}
		
		@Override
		protected Void doInBackground(Void... arg0) {
			data = jsonParser.sendJSON(server_url, json); // ERM WHAT? USELESS...
			
			return null;
		}
		
		@Override
		protected void onPreExecute(){
			dialog = ProgressDialog.show(RegisterActivity.this, "", "Registering new user...", true);
			dialog.setCancelable(false);
			dialog.setOnCancelListener(new OnCancelListener(){

				@Override
				public void onCancel(DialogInterface dialog) {

				}
				
			});
		}
		
		@Override
		protected void onPostExecute(Void result){
			dialog.dismiss();
			Toast.makeText(getApplicationContext(), "New user created!!!", Toast.LENGTH_LONG).show();
			
			finish();
		}
	}
	// COPY FROM UserLoginActivty ends here

}
